package com.example.kingdeescan.ui.dtype;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.example.kingdeescan.R;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

public class DTypeFragment extends Fragment {

    private DTypeViewModel dTypeViewModel;
    private EditText mEditText;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        dTypeViewModel =
                ViewModelProviders.of(this).get(DTypeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_dtype, container, false);
        final TextView textView = root.findViewById(R.id.text_dtype);
        dTypeViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        return root;
    }

    @Override
    public void onStart() {
        super.onStart();

        initComponet();
    }

    private void initComponet(){
        mEditText=getActivity().findViewById(R.id.home_edit);
        mEditText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                Log.i("msg", "keyCode:" + keyCode + ",   keyEvent.getAction:" + event.getAction());
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {

                    String barcode = mEditText.getText().toString().replaceAll("\n", "");
                    if ("".equals(barcode)) {
                        Toast.makeText(getContext(),"扫码为空",Toast.LENGTH_SHORT).show();
//                        UtilCommon.showToastShort("扫码为空");
                        return true;
                    }

                    //在这里实现自己的逻辑代码
                    //。。。。。。。。。。。。。

                }

                return false;
            }
        });
    }
}